
package JaggSoft;

import JaggSoft.Utilidades.Conexion;
import JaggSoft.Utilidades.MyModeloTabla;
import JaggSoft.Utilidades.Util;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JTable;

public class Solicitante extends javax.swing.JInternalFrame {
     private int codigo_solicitante;
     private boolean nuevo;
     
    public Solicitante() {
        initComponents();
        this.setLocation(new Point(70,0));
        Bloquear(true);
        BloquearTexto(true);
        CargarSolicitantes();
        codigo_solicitante=-1;
    }
 private void Bloquear(boolean estado){
        jbtnNuevo.setEnabled(estado);
        jbtnGuardar.setEnabled(!estado);
        jbtnModificar.setEnabled(estado);
        jbtnEliminar.setEnabled(estado);
        jbtnCancelar.setEnabled(!estado);
        jtblSolicitante.setEnabled(estado);
    }
    private void BloquearTexto(boolean estado){
        jtfNombre.setEditable(!estado);
        jtfTelefono.setEditable(!estado);
        jtfApellido.setEditable(!estado);
        jtfIdentidad.setEditable(!estado);
       
    }
    private void Limpiar(){
        jtfNombre.setText("");
        jtfTelefono.setText("");
        jtfApellido.setText(""); 
        jtfIdentidad.setText("");
    }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jtfIdentidad1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNombre = new javax.swing.JTextField();
        jtfApellido = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfIdentidad = new javax.swing.JTextField();
        jtfTelefono = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jbtnCancelar = new javax.swing.JButton();
        jbtnEliminar = new javax.swing.JButton();
        jbtnModificar = new javax.swing.JButton();
        jbtnGuardar = new javax.swing.JButton();
        jbtnNuevo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblSolicitante = new javax.swing.JTable();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setClosable(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Solicitante"));

        jLabel1.setText("Nombres");

        jLabel2.setText("Apellidos");

        jLabel3.setText("Identidad");

        jLabel4.setText("Telefono");

        jtfIdentidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfIdentidadKeyPressed(evt);
            }
        });

        jtfTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfTelefonoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfIdentidad, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfIdentidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Acciones"));

        jbtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_cancelar.png"))); // NOI18N
        jbtnCancelar.setToolTipText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_eliminar.png"))); // NOI18N
        jbtnEliminar.setToolTipText("Eliminar");
        jbtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarActionPerformed(evt);
            }
        });

        jbtnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_modificar.png"))); // NOI18N
        jbtnModificar.setToolTipText("Modificar");
        jbtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnModificarActionPerformed(evt);
            }
        });

        jbtnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_guardar.png"))); // NOI18N
        jbtnGuardar.setToolTipText("Guardar");
        jbtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGuardarActionPerformed(evt);
            }
        });

        jbtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_nuevo.png"))); // NOI18N
        jbtnNuevo.setToolTipText("Nuevo");
        jbtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jScrollPane2.setViewportView(jtblSolicitante);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 private void CargarSolicitantes(){
        String[] columnas={"Id","Identidad","Nombre","Apellido","Telefono"};
        Object[] datos=new Object[5];
        MyModeloTabla modelo=new MyModeloTabla(columnas);
        JTable tabla=Conexion.ObtenerTabla("SELECT id_solicitante,identidad,nombres,apellidos,telefono FROM solicitante",columnas);
        for(int i=0;i<tabla.getRowCount();i++){
            for(int j=0;j<columnas.length;j++)
                datos[j]=tabla.getValueAt(i,j);
            modelo.AgregarFila(datos);
        }
    
        jtblSolicitante.setModel(tabla.getModel());
       jtblSolicitante.getColumnModel().getColumn(0).setMaxWidth(0);
       jtblSolicitante.getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
       jtblSolicitante.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
       jtblSolicitante.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
       jtblSolicitante.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
           
        datos=null;
        tabla=null;
    }
          private void SeleccionarDatos(){
        int fila=jtblSolicitante.getSelectedRow();
        if(fila!=-1){
            codigo_solicitante=(int)jtblSolicitante.getValueAt(fila,0);
            jtfIdentidad.setText((String)jtblSolicitante.getValueAt(fila,1));
            jtfNombre.setText((String)jtblSolicitante.getValueAt(fila,2));
            jtfApellido.setText((String)jtblSolicitante.getValueAt(fila,3));
            jtfTelefono.setText((String)jtblSolicitante.getValueAt(fila,4));
        }
    }
    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        Limpiar();
        Bloquear(true);
        BloquearTexto(true);
        SeleccionarDatos();
    }//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jbtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarActionPerformed
        if(jtblSolicitante.getSelectedRow()!=-1){
            if(JOptionPane.showConfirmDialog(null,"¿Esta seguro de eliminar el solicitante?","Administración de Solicitantes",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                if(Conexion.EjecutarConsulta("DELETE FROM solicitante Where id_solicitante="+jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),0))){
                    CargarSolicitantes();
                    JOptionPane.showMessageDialog(null,"¡Solicitante Eliminado!","Administración de Solicitantes",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
        else
        JOptionPane.showMessageDialog(null,"Debe seleccionar un solicitante","Administración de Solicitantes",JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_jbtnEliminarActionPerformed

    private void jbtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnModificarActionPerformed
        jtfIdentidad.setText((String)jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),1));
        jtfNombre.setText((String)jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),2));
        jtfApellido.setText((String)jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),3));
        jtfTelefono.setText((String)jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),4));
      
        Bloquear(false);
        BloquearTexto(false);
        nuevo=false;
    }//GEN-LAST:event_jbtnModificarActionPerformed

    private void jbtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGuardarActionPerformed
        if(nuevo){
if( !jtfNombre.getText().equals("") && !jtfApellido.getText().equals("") && !jtfIdentidad.getText().equals("") && !jtfTelefono.getText().equals("")){
            if(Conexion.EjecutarConsulta("INSERT INTO solicitante(nombres,apellidos,telefono,identidad) "
                    + "VALUES('"+jtfNombre.getText().replace("'","\'")+"','"+jtfApellido.getText().replace("'","\'")+"','"+jtfTelefono.getText().replace("'","\'")+"','"+jtfIdentidad.getText().replace("'","\'")+"');")){
               
                CargarSolicitantes();
                JOptionPane.showMessageDialog(null,"¡Solicitante Agregado!","Administración de Solicitantes",JOptionPane.INFORMATION_MESSAGE);
            }
            else
            JOptionPane.showMessageDialog(null,"No se pudo agregar el solicitante","Administración de Solicitantes",JOptionPane.ERROR_MESSAGE);
        }
else {
JOptionPane.showMessageDialog(null,"Ingrese todos los datos solicitados","Administracion de Solicitantes",JOptionPane.ERROR_MESSAGE);
}
        }
        else{
            if(jtblSolicitante.getSelectedRow()!=-1 && !jtfNombre.getText().equals("") && !jtfApellido.getText().equals("") && !jtfIdentidad.getText().equals("") && !jtfTelefono.getText().equals("")){
                int id=(int)jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(),0);
                if(Conexion.EjecutarConsulta("UPDATE solicitante SET nombres='"+jtfNombre.getText().replace("'","\'")+"',apellidos='"+jtfApellido.getText().replace("'","\'")+"',telefono='"+jtfTelefono.getText().replace("'","\'")+"',identidad='"+jtfIdentidad.getText().replace("'","\'")+"' WHERE id_solicitante="+id)){
                    CargarSolicitantes();
                    JOptionPane.showMessageDialog(null,"¡Solicitante Modificado!","Administración de Solicitantes",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                JOptionPane.showMessageDialog(null,"No se pudo modificar el solicitante","Administración de Solicitantes",JOptionPane.ERROR_MESSAGE);
            }
            else
            JOptionPane.showMessageDialog(null,"Debe seleccionar un solicitante","Administración de Solicitantes",JOptionPane.WARNING_MESSAGE);
        }
        Bloquear(true);
        BloquearTexto(true);
        Limpiar();
    }//GEN-LAST:event_jbtnGuardarActionPerformed

    private void jbtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        Limpiar();
        nuevo=true;
    }//GEN-LAST:event_jbtnNuevoActionPerformed

    private void jtfIdentidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfIdentidadKeyPressed
        if(!Util.esNumero(jtfIdentidad.getText()+ evt.getKeyChar(), false)){
        evt.consume();
    }
    }//GEN-LAST:event_jtfIdentidadKeyPressed

    private void jtfTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTelefonoKeyPressed
        if(!Util.esNumero(jtfTelefono.getText()+ evt.getKeyChar(), false)){
        evt.consume();
    }
    }//GEN-LAST:event_jtfTelefonoKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JButton jbtnGuardar;
    private javax.swing.JButton jbtnModificar;
    private javax.swing.JButton jbtnNuevo;
    private javax.swing.JTable jtblSolicitante;
    private javax.swing.JTextField jtfApellido;
    private javax.swing.JTextField jtfIdentidad;
    private javax.swing.JTextField jtfIdentidad1;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JTextField jtfTelefono;
    // End of variables declaration//GEN-END:variables
}
